
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 382499
 */
public class Nursepanel extends javax.swing.JFrame {

    String prNRICType, prNRIC, prNRICLetter, prName, prDOB, prGender, prAddress, prPostal, prContact;
    DefaultTableModel model;
    JFileChooser prfc;
    File file = null;

    /**
     * Creates new form Nursepanel
     */
    public Nursepanel() {
        initComponents();
        CurrentDate();

        model = new DefaultTableModel();
        patientRecordTable.setModel(model);
        model.addColumn("NRIC/Passport Type");
        model.addColumn("NRIC/Passport");
        model.addColumn("NRIC/Passport Last Letter");
        model.addColumn("Name");
        model.addColumn("Date of Birth");
        model.addColumn("Gender");
        model.addColumn("Address");
        model.addColumn("Postal Code");
        model.addColumn("Contact No");
        
        
        displayPatientNRIC.setText((String) patientRecordNRICComboBox1.getSelectedItem());

    }

    public void CurrentDate() {
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        currentDateLabel.setText("Date: " + day + "/" + (month + 1) + "/" + year);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        patientRecordGenderbuttonGroup = new javax.swing.ButtonGroup();
        nurseOptions = new javax.swing.JTabbedPane();
        clinicManager = new javax.swing.JPanel();
        patientRecordNRICLabel = new javax.swing.JLabel();
        patientRecordNRICComboBox1 = new javax.swing.JComboBox();
        patientRecordNRICTextField = new javax.swing.JTextField();
        patientRecordNRICComboBox2 = new javax.swing.JComboBox();
        patientRecordNameLabel = new javax.swing.JLabel();
        patientRecordNameTextField = new javax.swing.JTextField();
        patientRecordDOBLabel = new javax.swing.JLabel();
        patientRecordGenderLabel = new javax.swing.JLabel();
        patientRecordGenderMaleRadioButton = new javax.swing.JRadioButton();
        patientRecordGenderFemaleRadioButton = new javax.swing.JRadioButton();
        patientRecordAddressLabel = new javax.swing.JLabel();
        patientRecordAddressTextField = new javax.swing.JTextField();
        patientRecordPostalCodeLabel = new javax.swing.JLabel();
        patientRecordPostalCodeTextField = new javax.swing.JTextField();
        patientRecordContactLabel = new javax.swing.JLabel();
        patientRecordContactTextField = new javax.swing.JTextField();
        patientRecordSearchButton = new javax.swing.JButton();
        patientRecordSaveButton = new javax.swing.JButton();
        patientRecordEditButton = new javax.swing.JButton();
        patientRecordNewButton = new javax.swing.JButton();
        patientRecordClearButton = new javax.swing.JButton();
        patientRecordDeleteButton = new javax.swing.JButton();
        patientRecordDisplayButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        patientRecordTable = new javax.swing.JTable();
        patientRecordExitButton = new javax.swing.JButton();
        currentDateLabel = new javax.swing.JLabel();
        patientRecordDOBTextField = new javax.swing.JTextField();
        patientRecordGenderComboBox = new javax.swing.JComboBox();
        displayPatientNRIC = new javax.swing.JLabel();
        displayNRICIntoLabel = new javax.swing.JButton();
        dispenseMedicine = new javax.swing.JPanel();
        medicineManager = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        clinicManager.setToolTipText("");

        patientRecordNRICLabel.setText("NRIC/Passport:");

        patientRecordNRICComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "S", "G", "F", "T" }));

        patientRecordNRICTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                patientRecordNRICTextFieldKeyTyped(evt);
            }
        });

        patientRecordNRICComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }));

        patientRecordNameLabel.setText("Name:");

        patientRecordNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                patientRecordNameTextFieldKeyTyped(evt);
            }
        });

        patientRecordDOBLabel.setText("Date of Birth:");

        patientRecordGenderLabel.setText("Gender:");

        patientRecordGenderbuttonGroup.add(patientRecordGenderMaleRadioButton);
        patientRecordGenderMaleRadioButton.setText("Male");

        patientRecordGenderbuttonGroup.add(patientRecordGenderFemaleRadioButton);
        patientRecordGenderFemaleRadioButton.setText("Female");

        patientRecordAddressLabel.setText("Address:");

        patientRecordPostalCodeLabel.setText("Postal Code:");

        patientRecordPostalCodeTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                patientRecordPostalCodeTextFieldKeyTyped(evt);
            }
        });

        patientRecordContactLabel.setText("Contact No:");

        patientRecordContactTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                patientRecordContactTextFieldKeyTyped(evt);
            }
        });

        patientRecordSearchButton.setText("Search Patient Record");
        patientRecordSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patientRecordSearchButtonActionPerformed(evt);
            }
        });

        patientRecordSaveButton.setText("Save Patient Record");
        patientRecordSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patientRecordSaveButtonActionPerformed(evt);
            }
        });

        patientRecordEditButton.setText("Edit Patient Record");
        patientRecordEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patientRecordEditButtonActionPerformed(evt);
            }
        });

        patientRecordNewButton.setText("New Patient Record");

        patientRecordClearButton.setText("Clear Content");
        patientRecordClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patientRecordClearButtonActionPerformed(evt);
            }
        });

        patientRecordDeleteButton.setText("Delete Patient Record");

        patientRecordDisplayButton.setText("Display all Patient Records in Table");
        patientRecordDisplayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patientRecordDisplayButtonActionPerformed(evt);
            }
        });

        patientRecordTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9"
            }
        ));
        patientRecordTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                patientRecordTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(patientRecordTable);

        patientRecordExitButton.setText("Exit");

        currentDateLabel.setText("Current Date");

        patientRecordGenderComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Select Gender-", "Male", "Female" }));

        displayPatientNRIC.setText("Display NRIC");

        displayNRICIntoLabel.setText("Display NRIC in label");
        displayNRICIntoLabel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                displayNRICIntoLabelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout clinicManagerLayout = new javax.swing.GroupLayout(clinicManager);
        clinicManager.setLayout(clinicManagerLayout);
        clinicManagerLayout.setHorizontalGroup(
            clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(clinicManagerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(clinicManagerLayout.createSequentialGroup()
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(patientRecordNRICLabel)
                            .addComponent(patientRecordNameLabel)
                            .addComponent(patientRecordDOBLabel)
                            .addComponent(patientRecordGenderLabel)
                            .addComponent(patientRecordAddressLabel)
                            .addComponent(patientRecordPostalCodeLabel)
                            .addComponent(patientRecordContactLabel))
                        .addGap(18, 18, 18)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(clinicManagerLayout.createSequentialGroup()
                                .addComponent(patientRecordNRICComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(patientRecordNRICTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(patientRecordNRICComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(patientRecordAddressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(patientRecordNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(patientRecordPostalCodeTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                .addComponent(patientRecordContactTextField, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGroup(clinicManagerLayout.createSequentialGroup()
                                .addComponent(patientRecordDOBTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(200, 200, 200)
                                .addComponent(displayPatientNRIC, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(clinicManagerLayout.createSequentialGroup()
                                .addComponent(patientRecordGenderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(98, 98, 98)
                                .addComponent(displayNRICIntoLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(patientRecordSearchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordSaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordEditButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordNewButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordClearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordDeleteButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(patientRecordDisplayButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, clinicManagerLayout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(patientRecordGenderMaleRadioButton)
                        .addGap(18, 18, 18)
                        .addComponent(patientRecordGenderFemaleRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(currentDateLabel)
                        .addGap(18, 18, 18)
                        .addComponent(patientRecordExitButton)))
                .addContainerGap())
        );
        clinicManagerLayout.setVerticalGroup(
            clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(clinicManagerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(patientRecordNRICLabel)
                    .addComponent(patientRecordNRICComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(patientRecordNRICTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(patientRecordNRICComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(patientRecordSearchButton))
                .addGap(18, 18, 18)
                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(patientRecordNameLabel)
                    .addComponent(patientRecordNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(patientRecordNewButton))
                .addGap(18, 18, 18)
                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(clinicManagerLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(patientRecordDOBLabel)
                            .addComponent(patientRecordDOBTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(displayPatientNRIC, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(patientRecordSaveButton))
                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(clinicManagerLayout.createSequentialGroup()
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(clinicManagerLayout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(patientRecordGenderLabel)
                                    .addComponent(patientRecordGenderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(clinicManagerLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(patientRecordDeleteButton)))
                        .addGap(18, 18, 18)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(patientRecordAddressLabel)
                            .addComponent(patientRecordAddressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(patientRecordEditButton))
                        .addGap(18, 18, 18)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(patientRecordPostalCodeLabel)
                            .addComponent(patientRecordPostalCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(patientRecordClearButton))
                        .addGap(18, 18, 18)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(patientRecordContactLabel)
                            .addComponent(patientRecordContactTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(patientRecordDisplayButton))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(patientRecordGenderMaleRadioButton)
                                .addComponent(patientRecordGenderFemaleRadioButton))
                            .addGroup(clinicManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(patientRecordExitButton)
                                .addComponent(currentDateLabel))))
                    .addGroup(clinicManagerLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(displayNRICIntoLabel)))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        nurseOptions.addTab("Clinic Manager", clinicManager);

        javax.swing.GroupLayout dispenseMedicineLayout = new javax.swing.GroupLayout(dispenseMedicine);
        dispenseMedicine.setLayout(dispenseMedicineLayout);
        dispenseMedicineLayout.setHorizontalGroup(
            dispenseMedicineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 971, Short.MAX_VALUE)
        );
        dispenseMedicineLayout.setVerticalGroup(
            dispenseMedicineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 470, Short.MAX_VALUE)
        );

        nurseOptions.addTab("Dispense Medicine", dispenseMedicine);

        javax.swing.GroupLayout medicineManagerLayout = new javax.swing.GroupLayout(medicineManager);
        medicineManager.setLayout(medicineManagerLayout);
        medicineManagerLayout.setHorizontalGroup(
            medicineManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 971, Short.MAX_VALUE)
        );
        medicineManagerLayout.setVerticalGroup(
            medicineManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 470, Short.MAX_VALUE)
        );

        nurseOptions.addTab("Medicine Manager", medicineManager);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nurseOptions)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nurseOptions))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void patientRecordSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientRecordSaveButtonActionPerformed
        // TODO add your handling code here:
        getAllPatientRecord();
        if (patientRecordNRICComboBox1.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Incomplete entry. Please select first NRIC/Passport type.", "Error", JOptionPane.ERROR_MESSAGE);
            patientRecordNRICComboBox1.requestFocus();
        } else {
            if (patientRecordNRICTextField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter NRIC/Passport.", "Error", JOptionPane.ERROR_MESSAGE);
                patientRecordNRICTextField.requestFocus();
            } else {
                if (patientRecordNRICComboBox2.getSelectedIndex() == 0) {
                    JOptionPane.showMessageDialog(this, "Incomplete entry. Please select last NRIC/Passport type.", "Error", JOptionPane.ERROR_MESSAGE);
                    patientRecordNRICComboBox2.requestFocus();
                } else {
                    if (patientRecordNameTextField.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter Name.", "Error", JOptionPane.ERROR_MESSAGE);
                        patientRecordNameTextField.requestFocus();
                    } else {
                        if (patientRecordDOBTextField.getText().isEmpty()) {
                            JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter DOB.", "Error", JOptionPane.ERROR_MESSAGE);
                            patientRecordDOBTextField.requestFocus();
                            //if (patientRecordDOBDateChooser.getDate() == null) {
                            //JOptionPane.showMessageDialog(this, "Incomplete entry. Please select Date of Birth.", "Error", JOptionPane.ERROR_MESSAGE);
                            //patientRecordDOBDateChooser.requestFocus();
                        } else {
                            if (patientRecordGenderComboBox.getSelectedIndex() == 0) {
                                JOptionPane.showMessageDialog(this, "Incomplete entry. Please select Gender", "Error", JOptionPane.ERROR_MESSAGE);
                                patientRecordGenderComboBox.requestFocus();
                                //if (patientRecordGenderbuttonGroup.getSelection() == null) {
                                //JOptionPane.showMessageDialog(this, "Incomplete entry. Please select Gender.", "Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                if (patientRecordAddressTextField.getText().isEmpty()) {
                                    JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter Address.", "Error", JOptionPane.ERROR_MESSAGE);
                                    patientRecordAddressTextField.requestFocus();
                                } else {
                                    if (patientRecordPostalCodeTextField.getText().isEmpty()) {
                                        JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter Postal Code.", "Error", JOptionPane.ERROR_MESSAGE);
                                        patientRecordPostalCodeTextField.requestFocus();
                                    } else {
                                        if (patientRecordContactTextField.getText().isEmpty()) {
                                            JOptionPane.showMessageDialog(this, "Incomplete entry. Please enter Contact No.", "Error", JOptionPane.ERROR_MESSAGE);
                                            patientRecordContactTextField.requestFocus();
                                        } else {
                                            prfc = new JFileChooser();
                                            int option = prfc.showSaveDialog(this);

                                            if (option == JFileChooser.APPROVE_OPTION) {
                                                model.addRow(new Object[]{prNRICType, prNRIC, prNRICLetter, prName, prDOB, prGender, prAddress, prContact});

                                                clear();
                                                file = prfc.getSelectedFile();
                                                try {
                                                    writeAll();
                                                } catch (IOException ex) {
                                                    Logger.getLogger(Nursepanel.class.getName()).log(Level.SEVERE, null, ex);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }//GEN-LAST:event_patientRecordSaveButtonActionPerformed

    public void getAllPatientRecord() {
        //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()); //To retreive and format date form jDateChooser1

        prNRIC = patientRecordNRICComboBox1.getSelectedItem().toString() + patientRecordNRICTextField.getText() + patientRecordNRICComboBox2.getSelectedItem().toString();
        System.out.println(prNRIC);
        prNRICType = patientRecordNRICComboBox1.getSelectedItem().toString();
        prNRIC = patientRecordNRICTextField.getText();
        prNRICLetter = patientRecordNRICComboBox2.getSelectedItem().toString();
        prName = patientRecordNameTextField.getText();
        //prDOB = sdf.format(patientRecordDOBDateChooser.getDate());
        prDOB = patientRecordDOBTextField.getText();
        prGender = patientRecordGenderComboBox.getSelectedItem().toString();

        //if (patientRecordGenderMaleRadioButton.isSelected()) {
        //    prGender = "Male";
        //} else {
        //    prGender = "Female";
        //}
        prAddress = patientRecordAddressTextField.getText();
        prPostal = patientRecordPostalCodeTextField.getText();
        prContact = patientRecordContactTextField.getText();
    }

    public void clear() {
        patientRecordNRICComboBox1.setSelectedIndex(0);
        patientRecordNRICTextField.setText("");
        patientRecordNRICComboBox2.setSelectedIndex(0);
        patientRecordNameTextField.setText("");
        //patientRecordDOBDateChooser.setDate(null);
        patientRecordDOBTextField.setText("");
        patientRecordGenderComboBox.setSelectedIndex(0);
        //patientRecordGenderbuttonGroup.clearSelection();
        patientRecordAddressTextField.setText("");
        patientRecordPostalCodeTextField.setText("");
        patientRecordContactTextField.setText("");

        if (patientRecordTable.getRowCount() > 0) {
            for (int i = patientRecordTable.getRowCount() - 1; i > -1; i--) {
                model.removeRow(i);
            }
        }
    }

    public void listenTable() {
        
        

/**
 *  Get the selected row using selectedRow(..) method. Append entire row to the String and set this string to the JTextFIeld.  
*/
    
        // Get the selected row from the table.
          int selectedRow = patientRecordTable.getSelectedRow();
          String setToTextField = new String();
          for(int i = 0; i < patientRecordTable.getColumnCount(); i++) {
              setToTextField += patientRecordTable.getValueAt(selectedRow, i) + "  ";
          }
          // Finally set this to your JTextField.
         patientRecordAddressTextField.setText(setToTextField);
 }
               
  

    private void writeAll() throws IOException {
        
        BufferedWriter buf;

        try {
            buf = new BufferedWriter(new FileWriter(file, true));
            buf.write(prNRICType + "," + prNRIC + "," + prNRICLetter + "," + prName + "," + prDOB + "," + prGender + "," + prAddress + "," + prPostal + "," + prContact);
            buf.newLine();
            buf.close();
            JOptionPane.showMessageDialog(this, "Patient Record Saved");
        } catch (Exception e) {
        }
    }

    private void patientRecordNRICTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_patientRecordNRICTextFieldKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_patientRecordNRICTextFieldKeyTyped

    private void patientRecordNameTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_patientRecordNameTextFieldKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_COMMA) || (c == KeyEvent.VK_SPACE))) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_patientRecordNameTextFieldKeyTyped

    private void patientRecordPostalCodeTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_patientRecordPostalCodeTextFieldKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_patientRecordPostalCodeTextFieldKeyTyped

    private void patientRecordContactTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_patientRecordContactTextFieldKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_patientRecordContactTextFieldKeyTyped

    private void patientRecordClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientRecordClearButtonActionPerformed
        // TODO add your handling code here:
        clear();
    }//GEN-LAST:event_patientRecordClearButtonActionPerformed

    private void patientRecordDisplayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientRecordDisplayButtonActionPerformed
        // TODO add your handling code here:
        InputStream is;

        try {
            File f = new File("C:\\Users\\Gulin\\Documents\\patient.txt");
            is = new FileInputStream(f);
            Scanner scan = new Scanner(is);
            String[] array;
            while (scan.hasNextLine()) {
                String line = scan.nextLine();

                if (line.indexOf(",") > -1) {
                    array = line.split(",");
                } else {
                    array = line.split("\n");
                }

                Object[] data = new Object[array.length];
                System.arraycopy(array, 0, data, 0, array.length);
                model.addRow(data);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "No Patient Record exist. Please check the file directory.", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_patientRecordDisplayButtonActionPerformed

    private void patientRecordEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientRecordEditButtonActionPerformed
        // TODO add your handling code here:
        model.setValueAt(patientRecordNRICComboBox1.getSelectedItem(), patientRecordTable.getSelectedRow(), 0);
        model.setValueAt(patientRecordNRICTextField.getText(), patientRecordTable.getSelectedRow(), 1);
        model.setValueAt(patientRecordNRICComboBox2.getSelectedItem(), patientRecordTable.getSelectedRow(), 2);
        model.setValueAt(patientRecordNameTextField.getText(), patientRecordTable.getSelectedRow(), 3);
        model.setValueAt(patientRecordDOBTextField.getText(), patientRecordTable.getSelectedRow(), 4);
        model.setValueAt(patientRecordGenderComboBox.getSelectedItem(), patientRecordTable.getSelectedRow(), 5);
        model.setValueAt(patientRecordAddressTextField.getText(), patientRecordTable.getSelectedRow(), 6);
        model.setValueAt(patientRecordPostalCodeTextField.getText(), patientRecordTable.getSelectedRow(), 7);
        model.setValueAt(patientRecordContactTextField.getText(), patientRecordTable.getSelectedRow(), 8);
    }//GEN-LAST:event_patientRecordEditButtonActionPerformed

    private void patientRecordTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_patientRecordTableMouseClicked
        // TODO add your handling code here:
        patientRecordNRICComboBox1.setSelectedItem(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 0)));
        patientRecordNRICTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 1)));
        patientRecordNRICComboBox2.setSelectedItem(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 2)));
        patientRecordNameTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 3)));
        patientRecordDOBTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 4)));
        patientRecordGenderComboBox.setSelectedItem(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 5)));
        patientRecordAddressTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 6)));
        patientRecordPostalCodeTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 7)));
        patientRecordContactTextField.setText(String.valueOf(model.getValueAt(patientRecordTable.getSelectedRow(), 8)));
    }//GEN-LAST:event_patientRecordTableMouseClicked

    private void displayNRICIntoLabelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_displayNRICIntoLabelActionPerformed
        // TODO add your handling code here:
        String letter = patientRecordNRICComboBox1.getSelectedItem().toString();
        String NRICNumber = patientRecordNRICTextField.getText();
        String letter2 = patientRecordNRICComboBox2.getSelectedItem().toString();
        displayPatientNRIC.setText(letter + NRICNumber );

//        System.out.println(totalNRIC);
        
        System.out.println(letter);
        System.out.println(NRICNumber);
        System.out.println(letter2);
        System.out.println(prNRIC);
        
    }//GEN-LAST:event_displayNRICIntoLabelActionPerformed

    private void patientRecordSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientRecordSearchButtonActionPerformed
        // TODO add your handling code here:
        
        try {
                File f = new File("C:\\Users\\Gulin\\Documents\\patient.txt");
                Scanner scan = new Scanner(new FileInputStream(f));
                String[] array;
                while (scan.hasNextLine()) {
                    String line = scan.nextLine();
                    if (line.indexOf(",") > -1) {
                        array = line.split(",");
                    } else {
                        array = line.split("\n");
                    }
                    Object[] data = new Object[array.length];
                    for (int i = 0; i < array.length; i++) {
                        data[i] = array[i];
                    }
                    
                    String letter = patientRecordNRICComboBox1.getSelectedItem().toString();
        String NRICNumber = patientRecordNRICTextField.getText();
        String letter2 = patientRecordNRICComboBox2.getSelectedItem().toString();
        displayPatientNRIC.setText(letter + NRICNumber );
                    
                    String totalNRIC = letter + NRICNumber + letter2;
                                        
                    if (line.contains(totalNRIC)) { 
                        System.out.println(line);
                    }
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "No such file.");
                displayPatientNRIC.requestFocus();
            }
    }//GEN-LAST:event_patientRecordSearchButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Nursepanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Nursepanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Nursepanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Nursepanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Nursepanel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel clinicManager;
    private javax.swing.JLabel currentDateLabel;
    private javax.swing.JPanel dispenseMedicine;
    private javax.swing.JButton displayNRICIntoLabel;
    private javax.swing.JLabel displayPatientNRIC;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel medicineManager;
    private javax.swing.JTabbedPane nurseOptions;
    private javax.swing.JLabel patientRecordAddressLabel;
    private javax.swing.JTextField patientRecordAddressTextField;
    private javax.swing.JButton patientRecordClearButton;
    private javax.swing.JLabel patientRecordContactLabel;
    private javax.swing.JTextField patientRecordContactTextField;
    private javax.swing.JLabel patientRecordDOBLabel;
    private javax.swing.JTextField patientRecordDOBTextField;
    private javax.swing.JButton patientRecordDeleteButton;
    private javax.swing.JButton patientRecordDisplayButton;
    private javax.swing.JButton patientRecordEditButton;
    private javax.swing.JButton patientRecordExitButton;
    private javax.swing.JComboBox patientRecordGenderComboBox;
    private javax.swing.JRadioButton patientRecordGenderFemaleRadioButton;
    private javax.swing.JLabel patientRecordGenderLabel;
    private javax.swing.JRadioButton patientRecordGenderMaleRadioButton;
    private javax.swing.ButtonGroup patientRecordGenderbuttonGroup;
    private javax.swing.JComboBox patientRecordNRICComboBox1;
    private javax.swing.JComboBox patientRecordNRICComboBox2;
    private javax.swing.JLabel patientRecordNRICLabel;
    private javax.swing.JTextField patientRecordNRICTextField;
    private javax.swing.JLabel patientRecordNameLabel;
    private javax.swing.JTextField patientRecordNameTextField;
    private javax.swing.JButton patientRecordNewButton;
    private javax.swing.JLabel patientRecordPostalCodeLabel;
    private javax.swing.JTextField patientRecordPostalCodeTextField;
    private javax.swing.JButton patientRecordSaveButton;
    private javax.swing.JButton patientRecordSearchButton;
    private javax.swing.JTable patientRecordTable;
    // End of variables declaration//GEN-END:variables
}
